package com.github.microwww.upms.service;

import com.github.microwww.upms.domain.User;
import com.github.microwww.upms.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.PageRequest;
import java.util.Optional;

@Service()
@Transactional()
public class UserService {

    @Autowired()
    private UserRepository userRepository;

    public UserRepository getUserRepository() {
        return userRepository;
    }

    public void setUserRepository(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public Page<User> findAll(int page, int size) {
        return this.userRepository.findAll(PageRequest.of(page, size));
    }

    public Optional<User> findById(int id) {
        return this.userRepository.findById(id);
    }

    public User getOrElseThrow(int id) {
        return this.userRepository.findById(id).orElseThrow(() -> {
            return new com.github.microwww.upms.exception.ExistException.NotExist(User.class);
        });
    }
}
